package cmd

import (
	"colly/models"
	"github.com/spf13/cobra"
)

func init() {
	rootCmd.AddCommand(daemonCmd)
}

var daemonCmd = &cobra.Command{
	Use:   "daemon",
	Short: "This is the daemon command",
	Long:  "This is the daemon command",
	Run: func(cmd *cobra.Command, args []string) {
		games := make([]models.Game, 0)
		Db.Where("enable = ?", 2).Find(&games)
		if len(games) < 1 {
			Log.Info("No Record Found")
			return
		}
		serverCmd, _, err := cmd.Root().Find([]string{"server"})
		if err != nil {
			Log.Panicln("Error finding world command:", err)
			return
		}

		for _, game := range games {
			serverCmd.SetArgs([]string{"--name=" + game.URL})
			err = serverCmd.Execute()
			if err != nil {
				Log.Error("Error Exec Command:", err)
				continue
			}
			Log.Info("Error Exec Command Success")
		}
	},
}
